<?php
class session_cart_l_cart extends a_base_cart_l_cart {

    protected $items;
    const SESS_CART = 'session_cart';
    public function __construct() {
        parent::__construct();
        $this->items = array();
    }

    public function is_in_cart($product_id) {
        return array_key_exists($product_id, $this->items) ? $this->items[$product_id] : NULL;
    }

    public function add_item($product_id, $quantity=1) {
        if (is_numeric($quantity) && $quantity > 0) {
            $p = new product_m_product();
            $p->get_by_id($product_id);

            if ($p->exists()) {
                $cart_item = NULL;
                if (($cart_item = $this->is_in_cart($product_id)) !== NULL) {
                    $cart_item->qty+=$quantity;
                    $this->add_update_item($product_id, $cart_item);
                } else {
                    $cart_item = new ses_cart_item_m_cart($product_id, $quantity);
                    $this->add_update_item($product_id, $cart_item);
                }
            } else {
                throw new product_not_found_exception_l_cart($product_id);
            }
        } else {
            throw new invalid_qty_exception_l_cart($product_id, $quantity);
        }
    }

    public function update_item($product_id, $new_qty) {
        if (is_numeric($new_qty) && $new_qty > 0) {
            $p = new product_m_product();
            if ($p->get_by_id($product_id)->exists()) {
                $cart_item = NULL;
                if (($cart_item = $this->is_in_cart($product_id)) !== NULL) {
                    $cart_item->qty =$new_qty;
                    $this->add_update_item($product_id, $cart_item);
                    return TRUE;
                } else {
                    throw new not_in_cart_exception_l_cart($product_id);
                }
            } else {
                $cart_item = NULL;
                if (($cart_item = $this->is_in_cart($product_id))) {
                    $this->unset_item($product_id);
                }
                throw new product_not_found_exception_l_cart($product_id);
            }
        } else {
            throw new invalid_qty_exception_l_cart($product_id, $new_qty);
        }
    }

    public function remove_item($product_id) {
        $cart_item = $this->is_in_cart($product_id);
        if ($cart_item) {
            $this->unset_item($product_id);
        } else {
            throw new not_in_cart_exception_l_cart($product_id);
        }
    }

    public function remove_all() {
        $this->items = array();
        $this->save();
    }

    public function is_empty() {
        return count($this->items) > 0 ? FALSE : TRUE;
    }

    public function total_items() {
        return count($this->items);
    }

    public function get_all_items() {
        return $this->items;
    }

    public static function i() {
        $saved_session_object = get_instance()->session->userdata(self::SESS_CART, TRUE);
        
        $obj = NULL;
        if (!$saved_session_object ) {
            $obj = new session_cart_l_cart();
            get_instance()->session->set_userdata(self::SESS_CART, $obj);
        } else {
            $obj = $saved_session_object;
        }
        
        return $obj;
    }

    public function save() {
        get_instance()->session->set_userdata(self::SESS_CART, $this);
        return $this;
    }

    private function add_update_item($index, ses_cart_item_m_cart $item) {
        $this->items[$index] = $item;
        $this->save();
    }

    private function unset_item($index) {
        $this->items[$index] = NULL;
        unset($this->items[$index]);
        $this->save();
    }

}

?>